package us.wili.dev.common.util.netease.im;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.wili.dev.common.util.httpclient.HttpClientUtil;
import us.wili.dev.common.util.netease.exception.NeteaseCodeEnum;
import us.wili.dev.common.util.netease.exception.NeteaseException;
import us.wili.dev.common.util.netease.exception.NeteaseIOException;
import us.wili.dev.common.util.netease.im.model.NeteaseBaseResponse;
import us.wili.dev.common.util.netease.im.model.NeteaseUserResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by JasonY on 17/4/21.
 */
public class NeteaseImUtil {

    private NeteaseProperties properties;
    private HttpClientUtil httpClientUtil;
    private CheckSumBuilder checkSumBuilder;

    private static final Logger logger = LoggerFactory.getLogger(NeteaseImUtil.class);

    private static final String NIMSERVER_URL_USER_CREATE = "https://api.netease.im/nimserver/user/create.action";
    private static final String NIMSERVER_URL_USER_UPDATE = "https://api.netease.im/nimserver/user/update.action";
    private static final String NIMSERVER_URL_USER_BLOCK = "https://api.netease.im/nimserver/user/block.action";
    private static final String NIMSERVER_URL_USER_UNBLOCK = "https://api.netease.im/nimserver/user/unblock.action";
    private static final String NIMSERVER_URL_USER_UPDATE_UINFO = "https://api.netease.im/nimserver/user/updateUinfo.action";
    private static final String NIMSERVER_URL_TEAM_CREATE = "https://api.netease.im/nimserver/team/create.action";
    private static final String NIMSERVER_URL_TEAM_ADD = "https://api.netease.im/nimserver/team/add.action";
    private static final String NIMSERVER_URL_TEAM_KICK = "https://api.netease.im/nimserver/team/kick.action";
    private static final String NIMSERVER_URL_TEAM_REMOVE = "https://api.netease.im/nimserver/team/remove.action";
    private static final String NIMSERVER_URL_TEAM_UPDATE = "https://api.netease.im/nimserver/team/update.action";
    private static final String NIMSERVER_URL_TEAM_CHANGE_OWNER = "https://api.netease.im/nimserver/team/changeOwner.action";
    private static final String NIMSERVER_URL_TEAM_MUTE = "https://api.netease.im/nimserver/team/muteTeam.action";
    private static final String NIMSERVER_URL_TEAM_LEAVE = "https://api.netease.im/nimserver/team/leave.action";

    public NeteaseImUtil(NeteaseProperties properties, HttpClientUtil httpClientUtil) {
        this.properties = properties;
        this.checkSumBuilder = new CheckSumBuilder();
        this.httpClientUtil = httpClientUtil;
    }

    public NeteaseImUtil(String appKey, String appSecret, HttpClientUtil httpClientUtil) {
        this.properties = new NeteaseProperties(appKey, appSecret);
        this.httpClientUtil = httpClientUtil;
        this.checkSumBuilder = new CheckSumBuilder();
    }

    protected String doPost(String url, Map<String, String> params) {
        String nonce = RandomStringUtils.random(12);
        long curTime = System.currentTimeMillis() / 1000;
        String checkSum = this.checkSumBuilder.getCheckSum(properties.getAppSecret(), nonce, curTime);

        Map<String, String> headers = new HashMap<>();
        headers.put("AppKey", properties.getAppKey());
        headers.put("Nonce", nonce);
        headers.put("CurTime", curTime + "");
        headers.put("CheckSum", checkSum);
        headers.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        HttpClientUtil.HttpResult httpResult = httpClientUtil.doPost(url, params, headers);

        if (HttpStatus.SC_OK != httpResult.getStatus()) {
            logger.info("请求状态： {}", httpResult.getStatus());
            throw new NeteaseIOException("请求失败");
        }

        String result = httpResult.getResult();
        NeteaseBaseResponse baseResponse = JSON.parseObject(result, NeteaseBaseResponse.class);
        if (!NeteaseCodeEnum.OK.getCode().equals(baseResponse.getCode())) {
            throw new NeteaseException(NeteaseCodeEnum.get(baseResponse.getCode()).getMessage());
        }

        return httpResult.getResult();
    }

    public NeteaseUserResponse createUser(Map<String, String> params) {
        String response = doPost(NIMSERVER_URL_USER_CREATE, params);
        NeteaseUserResponse userResponse = JSON.parseObject(response, NeteaseUserResponse.class);

        return userResponse;
    }

    public void blockUser(Map<String, String> params) {
        doPost(NIMSERVER_URL_USER_BLOCK, params);
    }

    public void unblockUser(Map<String, String> params) {
        doPost(NIMSERVER_URL_USER_UNBLOCK, params);
    }

    public void updateUserInfo(Map<String, String> params) {
        doPost(NIMSERVER_URL_USER_UPDATE_UINFO, params);
    }

    public String createTeam(Map<String, String> params) {
        String response = doPost(NIMSERVER_URL_TEAM_CREATE, params);
        JSONObject responseJsonObject = JSON.parseObject(response);

        return responseJsonObject.getString("tid");
    }

    public void addTeam(Map<String, String> params) {
        doPost(NIMSERVER_URL_TEAM_ADD, params);
    }

    public void kickTeam(Map<String, String> params) {
        doPost(NIMSERVER_URL_TEAM_KICK, params);
    }

    public void removeTeam(Map<String, String> params) {
        doPost(NIMSERVER_URL_TEAM_REMOVE, params);
    }

    public void updateTeam(Map<String, String> params) {
        doPost(NIMSERVER_URL_TEAM_UPDATE, params);
    }

    public void changeTeamOwner(Map<String, String> params) {
        doPost(NIMSERVER_URL_TEAM_CHANGE_OWNER, params);
    }

    public void muteTeam(Map<String, String> params) {
        doPost(NIMSERVER_URL_TEAM_MUTE, params);
    }

    public void leaveTeam(Map<String, String> params) {
        doPost(NIMSERVER_URL_TEAM_LEAVE, params);
    }
}
